<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { getLocalIcons, getSystemIcons } from '@/utils/icon';
import { ElMessageBox, ElMessage } from 'element-plus';

defineOptions({ name: 'IconPage' });

// 图标类型定义
interface IconItem {
  id: number;
  name: string;
}

// 本地图标列表
const localIcons = ref<IconItem[]>([]);
// 系统图标列表
const systemIcons = ref<IconItem[]>([]);
// 表格加载状态
const loading = ref(false);
// 当前正在编辑的图标
const currentIcon = ref<IconItem | null>(null);
// 编辑对话框可见性
const editDialogVisible = ref(false);
// 新图标名称
const newIconName = ref('');

// 初始化
onMounted(() => {
  loadIcons();
});

// 加载图标列表
function loadIcons() {
  loading.value = true;
  try {
    // 获取本地图标
    const localIconList = getLocalIcons();
    localIcons.value = localIconList.map((name, index) => ({
      id: index + 1,
      name
    }));
    
    // 获取系统图标
    const systemIconList = getSystemIcons();
    systemIcons.value = systemIconList.map((name, index) => ({
      id: index + 1,
      name
    }));
    
    console.log('本地图标:', localIcons.value);
    console.log('系统图标:', systemIcons.value);
  } catch (error) {
    console.error('加载图标失败:', error);
  } finally {
    loading.value = false;
  }
}

// 编辑图标
function handleEdit(row: IconItem) {
  currentIcon.value = row;
  newIconName.value = row.name;
  editDialogVisible.value = true;
}

// 保存图标修改
function handleSave() {
  if (!newIconName.value) {
    ElMessage.warning('图标名称不能为空');
    return;
  }
  
  if (currentIcon.value) {
    ElMessage.success(`图标 "${currentIcon.value.name}" 修改为 "${newIconName.value}" 成功`);
    editDialogVisible.value = false;
  }
}
</script>

<template>
  <div class="h-full">
    <!-- 本地图标表格 -->
    <el-card class="mb-16px">
      <template #header>
        <div class="flex justify-between items-center">
          <span>本地图标管理</span>
        </div>
      </template>
      
      <el-table :data="localIcons" border v-loading="loading" row-key="id">
        <el-table-column label="ID" prop="id" width="80" sortable />
        <el-table-column label="图标名称" prop="name" min-width="120" />
        <el-table-column label="图标预览" width="120">
          <template #default="{ row }">
            <div class="flex justify-center">
              <svg-icon :local-icon="row.name" class="text-30px text-primary" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleEdit(row)">
              修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="mt-16px">
        <p class="text-sm text-gray-500">图标位置：src/assets/local-icon/*.svg</p>
      </div>
    </el-card>
    
    <!-- 系统图标表格 -->
    <el-card>
      <template #header>
        <div class="flex justify-between items-center">
          <span>系统图标表格</span>
        </div>
      </template>
      
      <el-table :data="systemIcons" border v-loading="loading" row-key="id">
        <el-table-column label="ID" prop="id" width="80" sortable />
        <el-table-column label="图标名称" prop="name" min-width="120" />
        <el-table-column label="图标预览" width="120">
          <template #default="{ row }">
            <div class="flex justify-center">
              <svg-icon :icon="row.name" class="text-30px text-primary" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="mt-16px">
        <p class="text-sm text-gray-500">图标位置：src/assets/svg-icon/*.svg</p>
      </div>
    </el-card>
    
    <!-- 编辑对话框 -->
    <el-dialog v-model="editDialogVisible" title="修改图标" width="400px">
      <el-form label-position="top">
        <el-form-item label="图标名称">
          <el-input v-model="newIconName" placeholder="请输入新的图标名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="flex justify-end gap-10px">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
